package thu.cs.segroup.test;

import jade.lang.acl.ACLMessage;
import jade.lang.acl.UnreadableException;
import jade.core.Agent;
import jade.domain.FIPAAgentManagement.*;
import jade.domain.FIPAException;
import jade.domain.DFService;

import java.io.*;

import thu.cs.segroup.entity.TestTask;


public class Reader extends Agent {

	protected void setup() {
		/** Registration with the DF */
		DFAgentDescription dfd = new DFAgentDescription();    
		ServiceDescription sd = new ServiceDescription();
		sd.setType("ObjectReaderAgent"); 
		sd.setName(getName());
		sd.setOwnership("ExampleOfJADE");
		dfd.addServices(sd);
		dfd.setName(getAID());
		dfd.addOntologies("Test_Example");
		try {
			DFService.register(this,dfd);
		} catch (FIPAException e) {
			System.err.println(getLocalName()+" registration with DF unsucceeded. Reason: "+e.getMessage());
			doDelete();
		}
		/** End registration with the DF **/
		System.out.println(getLocalName()+ " succeeded in registration with DF");

		while (true) {
			try {
				System.out.println(getLocalName()+" is waiting for a message");
				ACLMessage msg = blockingReceive(); 
				System.out.println(getLocalName()+ " rx msg"+msg); 

				if ("JavaSerialization".equals(msg.getLanguage())) {
					TestTask p = (TestTask)msg.getContentObject();
					System.out.println(getLocalName()+ " read Java Object " + p.getClass().getName() + p.toString());
				} else
					System.out.println(getLocalName()+ " read Java String " + msg.getContent()); 

			} catch(UnreadableException e3){
				System.err.println(getLocalName()+ " catched exception "+e3.getMessage());
			}
		}
	}

	public void takeDown() {
		try {
			DFService.deregister(this);
		}
		catch (FIPAException e) {
			System.err.println(getLocalName()+" deregistration with DF unsucceeded. Reason: "+e.getMessage());
		}
	}

}
